import { BaseService } from './base.service';
import type {
  Project,
  CreateProjectDto,
  UpdateProjectDto,
  ProjectListParams,
  ProjectListResponse,
  ProjectStatistics,
} from '@/types/project';

/**
 * 项目管理服务
 */
class ProjectService extends BaseService {
  private readonly baseUrl = import.meta.env.VITE_API_URL || '/api/v1';

  /**
   * 获取项目列表
   */
  async getProjects(params?: ProjectListParams): Promise<ProjectListResponse> {
    try {
      return await this.get<ProjectListResponse>(`${this.baseUrl}/projects`, params);
    } catch (error) {
      console.error('Failed to fetch projects:', error);
      throw error;
    }
  }

  /**
   * 获取单个项目详情
   */
  async getProject(id: string): Promise<Project> {
    try {
      return await this.get<Project>(`${this.baseUrl}/projects/${id}`);
    } catch (error) {
      console.error(`Failed to fetch project ${id}:`, error);
      throw error;
    }
  }

  /**
   * 创建新项目
   */
  async createProject(data: CreateProjectDto): Promise<Project> {
    try {
      return await this.post<Project>(`${this.baseUrl}/projects`, data);
    } catch (error) {
      console.error('Failed to create project:', error);
      throw error;
    }
  }

  /**
   * 更新项目
   */
  async updateProject(id: string, data: UpdateProjectDto): Promise<Project> {
    try {
      return await this.put<Project>(`${this.baseUrl}/projects/${id}`, data);
    } catch (error) {
      console.error(`Failed to update project ${id}:`, error);
      throw error;
    }
  }

  /**
   * 删除项目
   */
  async deleteProject(id: string): Promise<void> {
    try {
      await this.delete<void>(`${this.baseUrl}/projects/${id}`);
    } catch (error) {
      console.error(`Failed to delete project ${id}:`, error);
      throw error;
    }
  }

  /**
   * 批量更新项目排序
   */
  async updateProjectsOrder(projectIds: string[]): Promise<void> {
    try {
      await this.post<void>(`${this.baseUrl}/projects/reorder`, {
        projectIds,
      });
    } catch (error) {
      console.error('Failed to update projects order:', error);
      throw error;
    }
  }

  /**
   * 获取项目统计信息
   */
  async getProjectStatistics(id: string): Promise<ProjectStatistics> {
    try {
      return await this.get<ProjectStatistics>(`${this.baseUrl}/projects/${id}/statistics`);
    } catch (error) {
      console.error(`Failed to fetch project statistics ${id}:`, error);
      throw error;
    }
  }

  /**
   * 复制项目
   */
  async duplicateProject(id: string, newName?: string): Promise<Project> {
    try {
      return await this.post<Project>(`${this.baseUrl}/projects/${id}/duplicate`, {
        name: newName,
      });
    } catch (error) {
      console.error(`Failed to duplicate project ${id}:`, error);
      throw error;
    }
  }

  /**
   * 归档项目
   */
  async archiveProject(id: string): Promise<Project> {
    try {
      return await this.post<Project>(`${this.baseUrl}/projects/${id}/archive`);
    } catch (error) {
      console.error(`Failed to archive project ${id}:`, error);
      throw error;
    }
  }

  /**
   * 恢复归档的项目
   */
  async unarchiveProject(id: string): Promise<Project> {
    try {
      return await this.post<Project>(`${this.baseUrl}/projects/${id}/unarchive`);
    } catch (error) {
      console.error(`Failed to unarchive project ${id}:`, error);
      throw error;
    }
  }
}

export const projectService = new ProjectService();
